/**
 * Created by hsiacool on 2017/8/18.
 */
'use strict';
import * as Sequelize from 'sequelize';
import {BaseDao} from "./BaseDao";
import {ArticleTypeModel} from "../models/index";

let model: Sequelize.Model<Sequelize.Instance<ArticleTypeModel>, ArticleTypeModel> = null;

class ArticleTypeDao extends BaseDao {

    static getModel(): Sequelize.Model<Sequelize.Instance<ArticleTypeModel>, ArticleTypeModel> {
        return model;
    }

    static find(params: any): Promise<Array<ArticleTypeModel>> {
        return new Promise<Array<ArticleTypeModel>>((resolve, reject) => {
            model.findAll({
                where: params,
            }).then((articles: Array<Sequelize.Instance<ArticleTypeModel>>) => {
                resolve(JSON.parse(JSON.stringify(articles)));
            }).error(reason => {
                reject(reason);
            })
        });
    }

    static count(params: any): Promise<number> {
        return new Promise<number>((resolve, reject) => {
            model.count({
                where: params
            }).then((count: number) => {
                resolve(count);
            }).error(reason => {
                reject(reason);
            })
        });
    }

    static insert(articleModel:ArticleTypeModel,options?: Sequelize.CreateOptions):Promise<ArticleTypeModel>{
        return new Promise<ArticleTypeModel>((resolve,reject) =>{
            model.create(articleModel,options).then((result:Sequelize.Instance<ArticleTypeModel>)=>{
                resolve(result.toJSON())
            }).error(reason => {
                reject(reason);
            })
        });
    }

    static update(articleType: ArticleTypeModel, t?: Sequelize.Transaction): Promise<Array<ArticleTypeDao>> {
        return new Promise<Array<ArticleTypeDao>>((resolve, reject) => {
            model.update(articleType, {
                transaction: t,
                where: {
                    id: articleType.id
                }
            }).then((result: any) => {
                resolve(result)
            }).catch(reason => {
                reject(reason);
            })
        });
    }
}

let schema = {
    cId:{type:Sequelize.NUMERIC,primaryKey:true,field:"c_id"},
    suppId:{type:Sequelize.NUMERIC,field:"supp_id"},
    parentId:{type:Sequelize.NUMERIC,field:"parent_id"},
    type:Sequelize.STRING,
    delFlag:{type:Sequelize.NUMERIC,field:"del_flag"},
    visible:Sequelize.NUMERIC,
    categName:{type:Sequelize.STRING,field:"categ_name"},
    createdDate:{type:Sequelize.DATE,field:"CREATED_DATE"}
};

let modelName: string = "DsArticleType";
let tableName: string = "comm_category";


let initDao = function (sequelize: Sequelize.Sequelize, DataTypes:any): Sequelize.Model<Sequelize.Instance<ArticleTypeModel>, ArticleTypeModel> {
    model = sequelize.define<Sequelize.Instance<ArticleTypeModel>, ArticleTypeModel>(modelName, schema, {
        createdAt: false,
        updatedAt: false,
        tableName: tableName
    });
    return model;
    // return [modelName,model];
};

export {ArticleTypeDao, initDao, modelName}
